package com.mxx.bu_yang.activity.on_line;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.adapter.StockInfoAdapter;
import com.mxx.bu_yang.adapter.StockStreamAdapter;
import com.mxx.bu_yang.common.CommonTitleActivity;
import com.mxx.bu_yang.response.StockDetailResponse;
import com.mxx.bu_yang.response.StockInfoResponse;
import com.mxx.common.config.ConstantsConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：刘江飞
 * 时间：2020/10/14  16:25
 * 邮箱：642371742Z@qq.com
 * 描述：
 **/
public class StockDetailActivity extends CommonTitleActivity {
    private RecyclerView rv_stock_info;
    private StockInfoAdapter infoAdapter;
    private RecyclerView tv_warehouse_scream;
    private StockStreamAdapter streamAdapter;
    private final Map<String, Object> paramsMap = new HashMap<>();

    @Override
    protected int getTitleType() {
        return TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_TEXT;
    }

    /**
     * 获取当前页面的布局文件
     *
     * @return int
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_stock_detail;
    }

    @Override
    public void initView() {
        super.initView();
        rv_stock_info = findViewById(R.id.rv_stock_info);
        initStockInfo();
        tv_warehouse_scream = findViewById(R.id.tv_warehouse_scream);
        initWarehouseScream();

        tv_title.setText("存货详情");
        tv_right.setText("首页");

        String value = (String) getSerValueByKey(ConstantsConfig.VALUE);
        paramsMap.put("value", value);
    }

    private void initStockInfo() {
        if (rv_stock_info.getItemDecorationCount() == 0) {
            RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    int mode = parent.getChildAdapterPosition(view);
                    if (mode == 0) {
                        outRect.top = 13;
                        outRect.bottom = 0;
                    } else {
                        outRect.top = 15;
                        if (mode == state.getItemCount() - 1) {
                            outRect.bottom = 15;
                        } else {
                            outRect.bottom = 0;
                        }
                    }
                }
            };
            rv_stock_info.addItemDecoration(decoration);
        }
        LinearLayoutManager mManager = new LinearLayoutManager(this);
        mManager.setOrientation(RecyclerView.VERTICAL);
        rv_stock_info.setLayoutManager(mManager);
        infoAdapter = new StockInfoAdapter(R.layout.adapter_stock_info);
        rv_stock_info.setAdapter(infoAdapter);
    }

    private void initWarehouseScream() {
        LinearLayoutManager mManager = new LinearLayoutManager(this);
        mManager.setOrientation(RecyclerView.VERTICAL);
        tv_warehouse_scream.setLayoutManager(mManager);
        streamAdapter = new StockStreamAdapter(R.layout.adapter_stock_stream);
        tv_warehouse_scream.setAdapter(streamAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        loadStockDetail();

    }

    private void loadStockDetail() {
        String value = (String) paramsMap.get("value");
        if (value == null || value.isEmpty()) {
            return;
        }
        StockDetailResponse response = new Gson().fromJson(value, new TypeToken<StockDetailResponse>() {
        }.getType());
        if (response == null) {
            return;
        }
        setStockInfo(response);
    }

    private void setStockInfo(StockDetailResponse response) {
        List<StockDetailResponse.ListBean> list = response.list;
        streamAdapter.setList(list);

        List<StockInfoResponse> stockInfoResponses = new ArrayList<>();
        StockInfoResponse stockInfoResponse;
        //物料编码
//        String materialNo = response.materialNo;
//        stockInfoResponse = new StockInfoResponse();
//        stockInfoResponse.key = "物料编码:";
//        stockInfoResponse.value = materialNo;
//        stockInfoResponses.add(stockInfoResponse);


        String lot_no = response.lot_no;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "批号:";
        stockInfoResponse.value = lot_no;
        stockInfoResponses.add(stockInfoResponse);
        //物料名称
        String materialName = response.materialName;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "物料名称:";
        stockInfoResponse.value = materialName;
        stockInfoResponses.add(stockInfoResponse);

        //规格型号
        String specification = response.specification;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "规格型号:";
        stockInfoResponse.value = specification;
        stockInfoResponses.add(stockInfoResponse);
        //现存量
        int num = response.num;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "现存量:";
        stockInfoResponse.value = num + "";
        stockInfoResponses.add(stockInfoResponse);
        //单位
        String unit = response.unit;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "单位:";
        stockInfoResponse.value = unit + "";
        stockInfoResponses.add(stockInfoResponse);

        infoAdapter.setList(stockInfoResponses);
    }

    @Override
    public void initListener() {
        super.initListener();
        tv_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.tv_right) {
            setResult(0x1001);
            finish();
        }

    }


    @Override
    protected void titleRightTextClick() {
        super.titleRightTextClick();
        int CHECK_IN_RESULT_CODE = 1010;
        setResult(CHECK_IN_RESULT_CODE, getIntent());
        finish();
    }
}
